package web.backend;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Easybuy_product_category;
import entity.Easybuy_user;
import service.EasyBuyService;
import utils.PageUtils;

/**
 * Servlet implementation class AdminUserServlet
 */
public class AdminUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private EasyBuyService user=new EasyBuyService();
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String action=request.getParameter("action");
		switch (action) {
		case "queryUserList":
			queryUserList(request,response);
			break;
		case "toUpdateUser":
			toUpdateUser(request,response);
			break;
		case "toAddUser":
			toAddUser(request,response);
			break;
		case "updateUser":
			updateUser(request,response);
			break;
		case "AddUser":
			AddUser(request,response);
			break;
		default:
			break;
		}
		
	}

	private void AddUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String loginName=request.getParameter("loginName");
		String userName=request.getParameter("userName");
		String pwd=request.getParameter("pwd");
		String sex=request.getParameter("sex");
		String identityCode=request.getParameter("identityCode");
		String email=request.getParameter("email");
		String mobile=request.getParameter("mobile");
		int type=Integer.parseInt(request.getParameter("type"));
		Easybuy_user user1=new Easybuy_user();
		user1.setEmail(email);
		user1.setIdentityCode(identityCode);
		user1.setLoginName(loginName);
		user1.setMobile(mobile);
		user1.setSex(sex);
		user1.setType(type);
		user1.setUserName(userName);
		user1.setPassword(pwd);
		
		PrintWriter out=response.getWriter();
		
		int result=user.regist(user1);
		
		out.print(result);
		out.close();
		
		
		
	}

	private void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String loginName=request.getParameter("loginName");
		String userName=request.getParameter("userName");
		String sex=request.getParameter("sex");
		String identityCode=request.getParameter("identityCode");
		String email=request.getParameter("email");
		String mobile=request.getParameter("mobile");
		int type=Integer.parseInt(request.getParameter("type"));
		int id=Integer.parseInt(request.getParameter("id"));
		
		PrintWriter out=response.getWriter();
		Easybuy_user user1=new Easybuy_user();
		user1.setEmail(email);
		user1.setId(id);
		user1.setIdentityCode(identityCode);
		user1.setLoginName(loginName);
		user1.setMobile(mobile);
		user1.setSex(sex);
		user1.setType(type);
		user1.setUserName(userName);
		
		int result=user.updateUser(user1);
		out.print(result);
		out.close();
	}

	private void toAddUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/backend/User/toUpdateUser.jsp").forward(request, response);
		
	}

	private void toUpdateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.parseInt(request.getParameter("id"));
		Easybuy_user user1=user.findById(id);
		request.setAttribute("user1", user1);
		request.getRequestDispatcher("/backend/User/toUpdateUser.jsp").forward(request, response);
		
	}

	private void queryUserList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int currpage=Integer.parseInt(request.getParameter("currpage"));
		int pagesize=6;
		
		PageUtils<Easybuy_user> utils=user.UserList(currpage, pagesize);
		request.setAttribute("utils", utils);
		utils.setPageindex(currpage);
		request.getRequestDispatcher("/backend/User/UserList.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
